<template>
  <div class="reply-form">
    <!-- 原消息引用 -->
    <div class="original-message">
      <div class="original-header">
        <span class="original-label">回复:</span>
        <span class="original-sender">{{ originalMessage.senderName }}</span>
        <span class="original-title">{{ originalMessage.title }}</span>
      </div>
      <div class="original-content">
        {{ originalMessage.content.substring(0, 100) }}
        <span v-if="originalMessage.content.length > 100">...</span>
      </div>
    </div>

    <!-- 回复表单 -->
    <el-form
      ref="replyFormRef"
      :model="replyForm"
      :rules="replyRules"
      label-width="80px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="replyForm.title"
          placeholder="请输入回复标题"
          maxlength="100"
          show-word-limit
        />
      </el-form-item>

      <el-form-item label="内容" prop="content">
        <el-input
          v-model="replyForm.content"
          type="textarea"
          :rows="6"
          placeholder="请输入回复内容"
          maxlength="1000"
          show-word-limit
        />
      </el-form-item>

      <!-- 附件上传 -->
      <el-form-item label="附件">
        <el-upload
          ref="uploadRef"
          :file-list="fileList"
          :on-change="handleFileChange"
          :on-remove="handleFileRemove"
          :before-upload="beforeUpload"
          :auto-upload="false"
          multiple
          drag
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">
            将文件拖到此处，或<em>点击上传</em>
          </div>
          <template #tip>
            <div class="el-upload__tip">
              支持jpg/png/gif/pdf/doc/docx文件，且不超过10MB
            </div>
          </template>
        </el-upload>
      </el-form-item>

      <!-- 快捷回复 -->
      <el-form-item label="快捷回复">
        <div class="quick-replies">
          <el-button
            v-for="quick in quickReplies"
            :key="quick.id"
            size="small"
            type="info"
            plain
            @click="insertQuickReply(quick.content)"
          >
            {{ quick.title }}
          </el-button>
        </div>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <div class="form-actions">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="sending" @click="handleSend">
        发送回复
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { UploadFilled } from '@element-plus/icons-vue'

// Props
const props = defineProps({
  originalMessage: {
    type: Object,
    required: true
  }
})

// Emits
const emit = defineEmits(['send', 'cancel'])

// 响应式数据
const replyFormRef = ref(null)
const uploadRef = ref(null)
const sending = ref(false)
const fileList = ref([])

const replyForm = reactive({
  title: `回复: ${props.originalMessage.title}`,
  content: '',
  receiverId: props.originalMessage.senderId,
  originalMessageId: props.originalMessage.id
})

// 表单验证规则
const replyRules = reactive({
  title: [
    { required: true, message: '请输入回复标题', trigger: 'blur' },
    { min: 2, max: 100, message: '标题长度在2-100个字符', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入回复内容', trigger: 'blur' },
    { min: 5, max: 1000, message: '内容长度在5-1000个字符', trigger: 'blur' }
  ]
})

// 快捷回复模板
const quickReplies = ref([
  { id: 1, title: '感谢回复', content: '感谢您的回复，我会认真考虑的。' },
  { id: 2, title: '需要更多信息', content: '请提供更多详细信息，以便我更好地了解情况。' },
  { id: 3, title: '同意', content: '我同意您的建议，我们可以按照这个方案进行。' },
  { id: 4, title: '需要时间考虑', content: '这个建议很好，但我需要一些时间来考虑。' },
  { id: 5, title: '约定时间', content: '我们可以约个时间详细讨论一下吗？' }
])

// 方法
const handleFileChange = (file, fileList) => {
  // 文件大小检查
  if (file.size > 10 * 1024 * 1024) {
    ElMessage.error('文件大小不能超过10MB')
    return false
  }
  
  // 文件类型检查
  const allowedTypes = [
    'image/jpeg', 'image/png', 'image/gif',
    'application/pdf',
    'application/msword',
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  ]
  
  if (!allowedTypes.includes(file.raw.type)) {
    ElMessage.error('不支持的文件类型')
    return false
  }
}

const handleFileRemove = (file, fileList) => {
  // 处理文件移除
}

const beforeUpload = (file) => {
  // 阻止自动上传
  return false
}

const insertQuickReply = (content) => {
  if (replyForm.content) {
    replyForm.content += '\n\n' + content
  } else {
    replyForm.content = content
  }
}

const handleSend = async () => {
  if (!replyFormRef.value) return
  
  try {
    const valid = await replyFormRef.value.validate()
    if (!valid) return
    
    sending.value = true
    
    // 准备发送数据
    const sendData = {
      ...replyForm,
      attachments: fileList.value.map(file => ({
        name: file.name,
        size: file.size,
        type: file.raw.type,
        file: file.raw
      }))
    }
    
    emit('send', sendData)
  } catch (error) {
    console.error('发送回复失败:', error)
  } finally {
    sending.value = false
  }
}

const handleCancel = () => {
  emit('cancel')
}
</script>

<style scoped>
.reply-form {
  padding: 20px 0;
}

.original-message {
  background: #f5f7fa;
  border-left: 4px solid #409eff;
  padding: 12px 16px;
  margin-bottom: 20px;
  border-radius: 6px;
}

.original-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 14px;
}

.original-label {
  color: #409eff;
  font-weight: 600;
}

.original-sender {
  color: #2c3e50;
  font-weight: 500;
}

.original-title {
  color: #606266;
}

.original-content {
  font-size: 13px;
  color: #909399;
  line-height: 1.4;
}

.quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid #ebeef5;
}

:deep(.el-upload-dragger) {
  width: 100%;
  height: 120px;
}

:deep(.el-upload__tip) {
  margin-top: 8px;
  font-size: 12px;
  color: #909399;
}
</style>
